﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Data; 

namespace websitev2.Core
{
    public class SqlAccessCore
    {

        public static string ExecuteScalar(string ExecutableSQL)
        {
            string _return = "";
            SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["startstop"].ConnectionString);
            thisConnection.Open();


            SqlCommand executeScaler = new SqlCommand(ExecutableSQL, thisConnection);

            if (executeScaler.ExecuteScalar() != null)
            {
                _return = executeScaler.ExecuteScalar().ToString();
            }

            thisConnection.Close(); 

            return _return;
        }


        public static DataTable ReturnDataTable(string ExecutableSQL)
        {
            string _SQL = ExecutableSQL;
            SqlDataAdapter da = new SqlDataAdapter();

            SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["startstop"].ConnectionString);
            thisConnection.Open();
            SqlCommand loadData = new SqlCommand(_SQL, thisConnection);
            DataTable _returnDataTable = new DataTable();
            da.SelectCommand = loadData;
            da.Fill(_returnDataTable);
            // always close the connection. :) 
            thisConnection.Close(); 
            
            return _returnDataTable;
        }

        public static DataTable ReturnDataTableNewDB(string ExecutableSQL)
        {
            string _SQL = ExecutableSQL;
            SqlDataAdapter da = new SqlDataAdapter();

            SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);
            thisConnection.Open();
            SqlCommand loadData = new SqlCommand(_SQL, thisConnection);
            DataTable _returnDataTable = new DataTable();
            da.SelectCommand = loadData;
            da.Fill(_returnDataTable);
            thisConnection.Close(); 
            return _returnDataTable;

        }



    }
}